package cn.frh.org.controller;

import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.util.AjaxResult;
import cn.frh.org.domain.Shop;
import cn.frh.org.query.ShopQuery;
import cn.frh.org.service.IShopService;
import cn.frh.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }
    //查询所有数据
    @GetMapping("/list")
    public List<Shop> findAll(){
        return shopService.queryAll();
    }

    @PostMapping("/page")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }
    @DeleteMapping("/{id}")
    public AjaxResult removeByUd(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getAdmin_id() == null){
                shopService.add(shop);

            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞 操作失败");
        }
    }

    @PostMapping("/settlement")
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        }
        catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞 操作失败");
        }
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }
}
